<?php
/**
 * EasyJs资源加载类
 * User: dxk
 * Date: 2020/07/17
 */
namespace App\Library\Service;
class EasyJs
{
    private static $config = [
        'js_alias' => [
            'bootstrap3' => 'vendor/bootstrap3/bootstrap.min.js',
            'bootstrap-editable' => 'vendor/bootstrap-editable/js/bootstrap-editable.min.js',
            'bootstrap-table' => 'vendor/bootstrap-table/bootstrap-table.js?v=1.0.2',
            'bootstrap-table.zh-cn' => 'vendor/bootstrap-table/locale/bootstrap-table-zh-CN.js',
            'bootstrap-table.filter-control' => 'vendor/bootstrap-table/extensions/filter-control/bootstrap-table-filter-control.js',
            'bootstrap-table.fixed-columns' => 'vendor/bootstrap-table/extensions/fixed-columns/bootstrap-table-fixed-columns.js',
            'bootstrap-table.treegrid' => 'vendor/bootstrap-table/extensions/treegrid/bootstrap-table-treegrid.min.js',
            'bootstrap-table.editable' => 'vendor/bootstrap-table/extensions/editable/bootstrap-table-editable.js',
            'bootstrap-table.page-jump-to' => 'vendor/bootstrap-table/extensions/page-jump-to/bootstrap-table-page-jump-to.js',
            'bootstrap-datepicker' => 'vendor/bootstrap-datepicker/js/bootstrap-datepicker.min.js',
            'bootstrap-datepickertime' => 'vendor/bootstrap-datepicker/js/bootstrap-datetimepicker.min.js',
            'bootstrap-datepickertime.zh-cn' => 'vendor/bootstrap-datepicker/locales/time/bootstrap-datetimepicker.zh-CN.js',
            'bootstrap-datepicker.zh-cn' => 'vendor/bootstrap-datepicker/locales/bootstrap-datepicker.zh-CN.min.js',
            'scroll' => 'vendor/jquery-slimscroll/jquery.slimscroll.min.js',
            'jquery-chosen' => 'vendor/jquery-chosen/chosen.jquery.min.js',
            'image-preview' => 'vendor/jquery.image-preview/jquery.image-preview.js',

            'jquery' => 'vendor/jquery/jquery.min.js?v=3.5.1',
            'jquery-form' => 'vendor/jquery-form/js/jquery.form.min.js?v=3.51.0',
            'jquery-treegrid' => 'vendor/jquery-treegrid/js/jquery.treegrid.min.js',
            'bootstrap' => 'vendor/bootstrap/js/bootstrap.min.js',
            'bootstrap.bundle' => 'vendor/bootstrap/js/bootstrap.bundle.min.js',
            'bootstrap-select' => 'vendor/bootstrap-select/js/bootstrap-select.min.js?v=1.13.14',
            'jquery-validation' => 'vendor/jquery-validation/jquery.validate.min.js?v=1.19.2',
            'jquery-validation-extend' => 'vendor/jquery-validation/extend/jquery.validate.extend.js?v=1.2.0',
            'layer' => 'vendor/layer/layer.js?v=3.1.1',
            'layui' => 'vendor/layui/layui.js?v=2.5.6',
            'site-menu' => 'vendor/site-menu/site-menu.js?v=1.2.0',
            'site-menu-contabs' => 'vendor/site-menu/site-menu-contabs.js?v=1.3.0',
            'site-top-menu' => 'vendor/site-menu/site-top-menu.js?v=1.2.0',
            'fullAvatarEditor' => 'vendor/fullavatareditor/scripts/fullAvatarEditor.js',
            'swfobject' => 'vendor/fullavatareditor/scripts/swfobject.js',
            'printer' => 'vendor/lodop/LodopFuncs.js?v=1.2.4',
            'table-input-ext' => 'vendor/table.input.ext.js?v=1.0.8',
            'switchery' => 'vendor/switchery/switchery.js',
        ],
        'css_alias' => [
            'bootstrap3' => 'vendor/bootstrap3/bootstrap.min.css',
            'bootstrap-editable' => 'vendor/bootstrap-editable/css/bootstrap-editable.css',
            'bootstrap-table' => 'vendor/bootstrap-table/bootstrap-table.min.css',
            'bootstrap-table.fixed-columns' => 'vendor/bootstrap-table/extensions/fixed-columns/bootstrap-table-fixed-columns.min.css',
            'bootstrap-table.filter-control' => 'vendor/bootstrap-table/extensions/filter-control/bootstrap-table-filter-control.min.css',
            'bootstrap-table.page-jump-to' => 'vendor/bootstrap-table/extensions/page-jump-to/bootstrap-table-page-jump-to.css',
            'bootstrap-datepicker' => 'vendor/bootstrap-datepicker/css/bootstrap-datepicker.min.css',
            'bootstrap-datepickertime' => 'vendor/bootstrap-datepicker/css/bootstrap-datetimepicker.min.css',
            'jquery-chosen' => 'vendor/jquery-chosen/chosen.css',
            'animate' => 'vendor/animate/animate.min.css',
            'animate-checkbox' => 'vendor/awesome-bootstrap-checkbox/awesome-bootstrap-checkbox.css',

            'jquery-treegrid' => 'vendor/jquery-treegrid/css/jquery.treegrid.css',
            'bootstrap' => 'vendor/bootstrap/css/bootstrap.min.css',
            'bootstrap-iframe' => 'vendor/bootstrap-iframe/bootstrap.min.css',
            'font-awesome' => 'vendor/font-awesome/css/font-awesome.css',
            'layui' => 'vendor/layui/css/layui.css?v=2.5.6',
            'web-icons-css' => 'vendor/web-icons/web-icons.css?v=0.2.4',
            'titatoggle' => 'vendor/titatoggle/titatoggle-dist.css',
            'switchery' => 'vendor/switchery/switchery.css',
        ]
    ];
    private static $css_version = '1.9';
    private static $js_version = '1.9';

    private static $css_cache = [];
    private static $js_cache = [];

    /**
     * 加载js
     * @param string|array $resource
     * @return string
     */
    public static function usejs($resource)
    {
        $list = [];
        if (!is_array($resource)) {
            $list[] = $resource;
        } else {
            $list = $resource;
        }
        $html = '';
        foreach ($list as $item) {
            $item = trim($item);
            if (empty($item)) {
                continue;
            }
            if (array_key_exists($item, self::$config['js_alias'])) {
                $item = self::$config['js_alias'][$item];
            }
            $url = easy_asset($item);
            if (str_ends_with($url, '.js') && strpos($url, '?') === false) {
                $url .= '?v=' . self::$js_version;
            }
            if (in_array(md5($url), self::$js_cache)) {
                continue;
            }
            self::$js_cache[] = md5($url);
            $html .= ' <script type="text/javascript" charset="utf-8" src="' . $url . '"></script>
            ';
        }
        return $html;
    }

    /**
     * 加载css
     * @param string|array $resource
     * @return string
     */
    public static function usecss($resource)
    {
        $list = [];
        if (!is_array($resource)) {
            $list[] = $resource;
        } else {
            $list = $resource;
        }
        $html = '';
        foreach ($list as $item) {
            $item = trim($item);
            if (empty($item)) {
                continue;
            }
            if (array_key_exists($item, self::$config['css_alias'])) {
                $item = self::$config['css_alias'][$item];
            }
            $url = easy_asset($item);
            if (str_ends_with($url, '.css') && strpos($url, '?') === false) {
                $url .= '?v=' . self::$css_version;
            }
            if (in_array(md5($url), self::$css_cache)) {
                continue;
            }
            self::$css_cache[] = md5($url);
            $html .= ' <link href="' . $url . '" rel="stylesheet">
            ';
        }
        return $html;
    }
}
